package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class CircularSlice extends AbilitySlice implements Component.ClickedListener {
    private Button button1, button2, button3, button4;
    private Image back_button;
    private Text text_title;
    private RoundProgressBar roundProgressBar1, roundProgressBar2, roundProgressBar3, roundProgressBar4;
    private AnimatorProperty animator1, animator2, animator3, animator4;
    private boolean isalpha1, isalpha2, isalpha3, isalpha4;
    private Thread myFristThread, mySecondThread;
    private int progress_fristThread = 1;
    private int progress_secondThread = 1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_circular);
        initView();
        initAnimation();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Circular progress")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Circular progress")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Circular progress", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Circular progress", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Circular progress", 1);
            pf.flushSync();
        }
    }

    private void initAnimation() {
        animator1 = roundProgressBar1.createAnimatorProperty();
        animator2 = roundProgressBar2.createAnimatorProperty();
        animator3 = roundProgressBar3.createAnimatorProperty();
        animator4 = roundProgressBar4.createAnimatorProperty();

//        myFristThread = new FristThread();
//        myFristThread.start();
//        mySecondThread = new SecondThread();
//        mySecondThread.start();

        animator1.setDuration(1200).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
        animator2.setDuration(800).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
        animator3.setDuration(800).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
        animator4.setDuration(800).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
    }

    class FristThread extends Thread {
        @Override
        public void run() {
//            super.run();
            while (progress_fristThread < 101) {
                try {
                    roundProgressBar1.setProgressValue(progress_fristThread++);
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (roundProgressBar1.getProgress() == 100) {
                animator1.alpha(0).setDuration(1000).start();
            }
        }
    }

    class SecondThread extends Thread {
        @Override
        public void run() {
//            super.run();
            while (progress_secondThread < 101) {
                try {
                    roundProgressBar2.setProgressValue(progress_secondThread++);
                    roundProgressBar3.setProgressValue(progress_secondThread++);
                    roundProgressBar4.setProgressValue(progress_secondThread++);
                    Thread.sleep(40);
                    if (progress_secondThread == 100) {
                        progress_secondThread = 1;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void initView() {
        roundProgressBar1 = (RoundProgressBar) findComponentById(ResourceTable.Id_round_progress_bar1);
        roundProgressBar2 = (RoundProgressBar) findComponentById(ResourceTable.Id_round_progress_bar2);
        roundProgressBar3 = (RoundProgressBar) findComponentById(ResourceTable.Id_round_progress_bar3);
        roundProgressBar4 = (RoundProgressBar) findComponentById(ResourceTable.Id_round_progress_bar4);

        button1 = (Button) findComponentById(ResourceTable.Id_button1);
        button2 = (Button) findComponentById(ResourceTable.Id_button2);
        button3 = (Button) findComponentById(ResourceTable.Id_button3);
        button4 = (Button) findComponentById(ResourceTable.Id_button4);

        button1.setClickedListener(this);
        button2.setClickedListener(this);
        button3.setClickedListener(this);
        button4.setClickedListener(this);
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setClickedListener(this);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Circular progress");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        animator1.reset();
        animator2.reset();
        animator3.reset();
        animator4.reset();
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
            animator1.reset();
            animator2.reset();
            animator3.reset();
            animator4.reset();
        } else if (component.getId() == Id_button1) {
            roundProgressBar1.setVisibility(isalpha1 ? Component.VISIBLE : Component.INVISIBLE);
            isalpha1 = !isalpha1;
        } else if (component.getId() == Id_button2) {
            roundProgressBar2.setVisibility(isalpha2 ? Component.VISIBLE : Component.INVISIBLE);
            isalpha2 = !isalpha2;
        } else if (component.getId() == Id_button3) {
            roundProgressBar3.setVisibility(isalpha3 ? Component.VISIBLE : Component.INVISIBLE);
            isalpha3 = !isalpha3;
        } else if (component.getId() == Id_button4) {
            roundProgressBar4.setVisibility(isalpha4 ? Component.VISIBLE : Component.INVISIBLE);
            isalpha4 = !isalpha4;
        }
    }
}
